#include "main.h"
#include "cmsis_os.h"
#include "string.h"
#include "swInfo.h"

struct
{
	uint8_t number; 										  //开关路数
	uint32_t conflag; 										//远控标志
	uint32_t state;												//开关状态
	uint32_t valid[SW_MAX_NUMBER];		//信息有效标志
	uint8_t info[SW_MAX_NUMBER][SW_INFO_BYTES]; //具体一个开关的其他信息，线路电压、漏电电流……
	uint8_t extras;															//附加信息,门、风扇状态	
}swInfo;

extern osMutexId swinfoMuxHandle;

void swinfo_init()
{
	memset((void *)&swInfo,0x00,sizeof(swInfo));
	swInfo.number=1;			
	for(uint8_t i=0;i<SW_MAX_NUMBER;i++)
	{
		swInfo.valid[i]=0;				 //清除所有信息有效标志
	}	
}

uint8_t swinfo_get_number()
{
	return swInfo.number;
}
uint32_t swinfo_get_conflag()
{
	return swInfo.conflag;
}
uint32_t swinfo_get_state()
{
	return swInfo.state;
}
uint8_t swinfo_get_extras()
{
	return swInfo.extras;
}
//设置开关路数
void swinfo_set_number(uint8_t value)
{
	swInfo.number=value;
}
//设置开关远程控制标志
void swinfo_set_conflag(uint32_t value)
{
	swInfo.conflag=value;
}
//设置开关状态
void swinfo_set_state(uint32_t value)
{
	swInfo.state=value;
}
//设置附加信息
void swinfo_set_extras(uint8_t value)
{
	swInfo.extras=value;
}
//获取某个开关的信息，获取失败返回0
uint8_t swinfo_get_info(uint8_t ch,uint8_t *buf)
{
//	if(swInfo.valid[ch]!=SW_VILID)
//	{
//		memset(buf,0,SW_INFO_BYTES);
//		return 0;
//	}
//	else 
	{
		osMutexWait(swinfoMuxHandle,osWaitForever);
		memcpy(buf,swInfo.info[ch],SW_INFO_BYTES);
		osMutexRelease(swinfoMuxHandle);
		
	}
	return SW_INFO_BYTES;
}
/*
@ brief:保存开关的参数到info表中，一次更新所有路
@	param funcAdd:功能地址
@	param buf:信息
@	param	swNum：一共几路开关
*/
void swinfo_set_info(uint8_t funcAdd,uint8_t *buf,uint8_t swNum)
{
	osMutexWait(swinfoMuxHandle,osWaitForever);
	for(uint8_t i=0;i<swNum;i++)
	{
		swInfo.valid[i]&=~(1<<funcAdd);
		swInfo.info[i][funcAdd*2]=buf[i*2];
		swInfo.info[i][funcAdd*2+1]=buf[i*2+1];
	}
	osMutexRelease(swinfoMuxHandle);	
}

